package com.scs.application.modules.dept.controller;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.scs.application.core.mvc.controller.CrudController;
import com.scs.application.core.query.QueryContext;
import com.scs.application.core.query.QueryContextHolder;
import com.scs.application.modules.dept.entity.UseDtl;
import com.scs.application.modules.dept.service.UseDtlService;
import com.scs.application.modules.sys.model.PageResult;
import com.scs.application.modules.sys.model.RestResult;
import com.scs.application.modules.wm.service.StockService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 */
@RestController
@RequestMapping("/dept/use_dtl")
public class UseDtlController extends CrudController<UseDtlService, UseDtl> {
    @Autowired
    private StockService stockService;
    /**
     * 添加/更新数据 定数包消耗
     */
    @PostMapping("/saveByUseSn")
    public RestResult saveByUseSn(@RequestBody Map<String, String> params) {
        List<String> stockIds = (List<String>) JSONArray.parse(params.get("stockIds"));
        service.saveByStockIds(stockIds, params.get("useId"), params.get("warehouseId"), false);
        return RestResult.ok("数据保存成功");
    }

    /***
     * @param params
     * @return: com.scs.application.modules.sys.model.RestResult
     * @Author: Tai
     * @date: 2024/4/29
     * @Description:  PDA使用
     */
    @PostMapping("/saveItemByCode")
    public RestResult saveItemByCode(@RequestBody Map<String, String> params) {
        List<String> stockIds = (List<String>) JSONArray.parse(params.get("stockIds"));
        for (String stockId : stockIds) {
            service.saveByStock(stockId, params.get("useId"), false);
        }
        return RestResult.ok("数据保存成功");
    }

    /**
     *  冲红选择耗材列表
     * @return
     */
    @GetMapping("/pageForRedList")
    public PageResult pageForRedList() {
        QueryContext queryContext = QueryContextHolder.currentContext();
        IPage pageResult = service.pageForRedList(queryContext);
        return PageResult.ok("查询成功", pageResult.getRecords(), pageResult.getTotal());
    }

    /**
     * 查询科室消耗单的耗材分类
     */
    @GetMapping("/getType")
    public RestResult getType(@RequestParam String useId) {
        return RestResult.ok("查询成功", service.getType(useId));
    }

}
